﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace GingerGL.IO
{
    //---------------------------------------------------------------//
    /// <summary>
    /// Path Class
    /// </summary>
    static public class Path
    {
        //-----------------------------------------------------------//
        #region Methods
        //---------------------------------------------------------------
        /// <summary>
        /// Extracts the folder path from a filePath. 
        /// </summary>
        static public string GetFolder(string filepath)
        {
            string path = Path.Unify(filepath);
            int index = path.LastIndexOf('/');
            if (index == -1 || index == path.Length - 1)
                return "";
            return path.Substring(0, index);
        }
        /// <summary>
        /// Extracts the fileName from a filePath.
        /// </summary>
        static public string GetFileName(string filePath)
        {
            string path = Path.Unify(filePath);
            int index = path.LastIndexOf('/');
            if (index == -1 || index == path.Length - 1)
                return path;
            return path.Substring(index + 1);
        }
        /// <summary>
        /// Extracts the fileName from a filePath.
        /// </summary>
        static public string GetFileNameWithoutExtension(string filename)
        {
            int index = filename.LastIndexOf('.');
            if (index == -1)
                return "";
            return filename.Substring(0, index);
        }
        /// <summary>
        /// Extracts the extension from a given filePath.
        /// </summary>
        static public string GetExtension(string filePath)
        {
            int index = filePath.LastIndexOf('.');
            if (index == -1)
                return "";
            return filePath.Substring(index + 1);
        }
        /// <summary>
        /// Tests an array of strings if they match a certain pattern.
        /// </summary>
        static public string[] Match(string[] names, string pattern)
        {
            Regex regex = new Regex(@"(^.*/|^)" + pattern, RegexOptions.IgnoreCase);
            ArrayList list = new ArrayList();
            for (int i = 0; i < names.Length; i++)
            {
                if (regex.IsMatch(names[i]))
                    list.Add(names[i]);
            }
            return (string[])list.ToArray(typeof(string));
        }
        /// <summary>
        /// Creates a unified path from a operating system dependant.
        /// </summary>
        static public string Unify(string path)
        {
            if (path.Length == 0)
                return path;
            path = path.Replace('\\', '/');
            if (path[path.Length - 1] == '/')
                return path.Substring(0, path.Length - 1);
            return path;
        }
        /// <summary>
        /// Unifies an array of pathes.
        /// </summary>
        static public string[] Unify(string[] pathes)
        {
            for (int i = 0; i < pathes.Length; i++)
                pathes[i] = Unify(pathes[i]);
            return pathes;
        }
        /// <summary>
        /// Merges two filePaths.
        /// </summary>
        static public string Merge
            (string filePath1, string filePath2)
        {
            if (filePath1.Length == 0)
                return Unify(filePath2);
            else if (filePath2.Length == 0)
                return Unify(filePath1);
            if (Path.IsFullPath(filePath2))
                throw new ArgumentException("FilePath2 is a full path, but filePath1 isn't empty!");
            string path = Unify(filePath1);
            return path + '/' + filePath2;
        }
        /// <summary>
        /// Tests if a given path is a already a full path.
        /// </summary>
        static public bool IsFullPath(string path)
        {
            return path.IndexOf(':') != -1;
        }
        /// <summary>
        /// Removes a certain subPath from a path.
        /// </summary>
        static public string Remove(string path, string subPath)
        {
            int index = path.IndexOf(subPath);
            if (index == -1)
                throw new ArgumentException("The subPath wasn't contained by the path!");
            return path.Substring(index + subPath.Length);
        }
        //---------------------------------------------------------------
        #endregion
        //-----------------------------------------------------------//
    }
    //---------------------------------------------------------------//
}
